﻿angular.module('app.authenticate').controller('ControlPanelController', ['$scope', 'authService', 'notify',
	function ($scope, authService, notify) {
		'use strict';

		var instance = this;
		// get the authenticated user
		$scope.user = authService.authStatus.authenticatedUser;
		// declare the fields on the model
		$scope.model = {
			nickname: '',
			email: '',
			password: '',
			confirmPassword: '',
			inputtype: 'password'
		};
		// determine when to show certain fields
		$scope.showLogIn = $scope.user === null || $scope.user === undefined;
		$scope.showUserLoggedIn = !$scope.showLogIn;
		$scope.showRegister = false;
		$scope.showResetPassword = false;
		// show the log in state
		this.showLogInState = function () {
			$scope.showLogIn = true;
			$scope.showUserLoggedIn = false;
			$scope.showRegister = false;
			$scope.showResetPassword = false;
		};
		// show logged in state
		this.showLoggedInState = function () {
			$scope.showLogIn = false;
			$scope.showUserLoggedIn = true;
			$scope.showRegister = false;
			$scope.showResetPassword = false;
		};
		// show register state
		this.showRegisterState = function () {
			$scope.showLogIn = false;
			$scope.showUserLoggedIn = false;
			$scope.showRegister = true;
			$scope.showResetPassword = false;
		};
		// show reset password state
		this.showResetPasswordState = function () {
			$scope.showLogIn = false;
			$scope.showUserLoggedIn = false;
			$scope.showRegister = false;
			$scope.showResetPassword = true;
		};
		// handle the reply from the log in
		this.handleLogInReply = function (reply) {
			// If authentication successful
			if (authService.authStatus.isAuthenticated) {
				// show logged in
				instance.showLoggedInState();
				// clear email
				$scope.model.email = '';
				// show message
				notify.success('Welcome Back');
			}
			else if (reply.data.Message.length > 0) {
				// show error message
				notify.error(reply.data.Message);
			}
			// set the local user reference
			$scope.user = reply.data.Content;
			// clear the password
			$scope.model.password = '';
		};
		// Handle reply for register
		this.handleRegisterReply = function (reply) {
			// if authenticated, registration successful
			if (authService.authStatus.isAuthenticated) {
				// show logged in state
				instance.showLoggedInState();
				// clear email and nickname
				$scope.model.email = '';
				$scope.model.nickname = '';
				// show message
				notify.success('Thank your for registering');
			}
			else {
				// show error message
				notify.error(reply.data.Message);
			}
			// set the local user email reference
			$scope.user = reply.data.Content;
			// clear registers password fields
			$scope.model.password = '';
			$scope.model.confirmPassword = '';
		};
		// handle password reset replay
		this.handlePasswordResetReply = function (reply) {
			// if successful
			if (reply.data.Successful) {
				// go to log in state and show message
				instance.showLogInState();
				notify.success(reply.data.Message);
			} else {
				// show error message
				notify.error(reply.data.Message);
			}
		};
		// log in functionality
		$scope.logIn = function () {
			var messages = [];
			// email is required
			if ($scope.model.email.length < 1) {
				messages.push('Email is required');
			}
			// password is required
			if ($scope.model.password.length < 1) {
				messages.push('Password is required');
			}
			// if message exist, don't send
			if (messages.length > 0) {
				// show messages and clear the user and email
				for (var index in messages) {
					notify.warning(messages[index]);
				}
				$scope.user = null;
				$scope.model.password = '';
			}
			else {
				// try to authenticate
				authService.authenticate($scope.model.email, $scope.model.password).then(instance.handleLogInReply);
			}
		};
		// log out functionality
		$scope.logOut = function () {
			// log out from auth service
			authService.logOut();
			// clear variables, show message, and go to log in state
			$scope.isLoggedIn = false;
			$scope.user = null;
			notify.warning('Logged Out');
			instance.showLogInState();
		};
		// show regisiter
		$scope.registerNewUser = function () {
			// show state and clear variables
			instance.showRegisterState();
			$scope.model.password = '';
			$scope.model.confirmPassword = '';
		};
		// cancel register
		$scope.cancelRegister = function () {
			// go to log in and clear variables
			instance.showLogInState();
			$scope.model.password = '';
			$scope.model.confirmPassword = '';
		};
		// create a new user
		$scope.createNewUser = function () {
			var pass = true;
			// handle required fields
			if ($scope.model.nickname.length < 1) {
				notify.warning('Nickname is required');
				pass = false;
			}

			if ($scope.model.email.length < 1) {
				notify.warning('Email is required');
				pass = false;
			}
			// passwords required and must match
			if ($scope.model.password.length < 1) {
				notify.warning('Password is required');
				pass = false;
			} else if ($scope.model.password !== $scope.model.confirmPassword) {
				notify.warning('Passwords do not match');
				pass = false;
			}
			// if failed validation, clear fields
			if (!pass) {
				$scope.user = null;
				$scope.model.password = '';
				$scope.model.confirmPassword = '';
			}
			else {
				// attempt registration
				authService.register($scope.model.nickname, $scope.model.email, $scope.model.password).then(instance.handleRegisterReply);
			}
		};
		// show reset password
		$scope.showPasswordReset = function () {
			instance.showResetPasswordState();
		}
		// reset password
		$scope.resetPassword = function () {
			// email required
			if ($scope.model.email.length < 1) {
				notify.warning('Email is required');
			}
			else {
				// send reset request
				authService.resetPassword($scope.model.email).then(instance.handlePasswordResetReply);
			}
		};

		// Try to log the user in through the cookie on initialization
		if ($scope.user === null || $scope.user === undefined) {
			authService.sendAuthRequest(null).then(instance.handleLogInReply);
		}
	}]);